package com.chuji.crmcj.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.act.MagClientLoseDetailActivity;
import com.chuji.crmcj.bean.MagOrderLoseInfo;
import com.chuji.crmcj.utils.NumberUtils;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.util.ArrayList;
import java.util.List;


public class MagOrderLoseFragment extends Fragment {
    //订单战败列表
    private ListView mLv_order_lose;
    /*自定义适配器*/
    private CustomOrderLoseAdapter customOrderLoseAdapter;
    /*数据存储*/
    List<MagOrderLoseInfo.ApiParamObjEntity> magOrderLoseData;
    /*网络数据*/
    private MagOrderLoseInfo magOrderLoseInfo;
    //用户ID
    String UserID;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_order_lose, container, false);
        mLv_order_lose = (ListView) view.findViewById(R.id.lv_order_lose);
        initData();
//        //初始化适配器
//        customOrderLoseAdapter=new CustomOrderLoseAdapter();
//        //数据
//
//        //填充
//        mLv_order_lose.setAdapter(customOrderLoseAdapter);
        return view;
    }
    public void initData() {
        /**
         * 获得评价记录
         */
        getAppraiseData();
    }
    private void getAppraiseData() {
        //第一次
        if (magOrderLoseData == null) {
            magOrderLoseData = new ArrayList<MagOrderLoseInfo.ApiParamObjEntity>();
        }
        requestData();
    }
    private void requestData() {
        //初始化Volley
        UserID= SPUtils.getString(UIUtils.getContext(), "UserID", "");
        String strURL = String.format(UrlUtils.GET_MAG_ORDER_LOSE+"&UserID="+"817C9EBC-BA19-48FD-BED9-1C991AB877B1"+"&defeatType=0"+"&pageSize="+"3"+"&currentPage="+"1");
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                magOrderLoseInfo = JSON.parseObject(response, MagOrderLoseInfo.class);
                magOrderLoseData.addAll(magOrderLoseInfo.getApiParamObj());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        // 判断adapter是否创建
                        if (customOrderLoseAdapter == null) {
                            customOrderLoseAdapter = new CustomOrderLoseAdapter();
                            mLv_order_lose.setAdapter(customOrderLoseAdapter);
                        } else {
                            customOrderLoseAdapter.notifyDataSetChanged();
                        }
                        //给ListView设置点击事件
                        mLv_order_lose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                //传递战败申请ID
                                Intent intent=new Intent(UIUtils.getContext(), MagClientLoseDetailActivity.class);
                                intent.putExtra("DefeatApproveID", magOrderLoseData.get(position).getDefeatApproveID());
                                startActivity(intent);
                            }
                        });
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }


    /**
     * 自定义适配器
     */
    class CustomOrderLoseAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return magOrderLoseData.size();
        }

        @Override
        public Object getItem(int position) {
            return magOrderLoseData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            /****1)根据list_item.xml--》得到当前子项元素的布局   【界面】*******/
            //b)声明ViewHolder
            ViewHolder viewHolder = null;
            //c)判断convertView是否为null
            if (convertView == null) {//第一次调用getView（）方法时为 null,意思就是在内存中没有
                viewHolder = new ViewHolder();
                convertView =  LayoutInflater.from(UIUtils.getContext()).inflate(R.layout.listview_mag_order_lose, null);
                //初始化组件
                viewHolder.mTv_client_name = (TextView)  convertView.findViewById(R.id.tv_client_name);
                viewHolder.mTv_client_phoneNumber = (TextView)  convertView.findViewById(R.id.tv_client_phoneNumber);
                viewHolder.mTv_sales = (TextView)  convertView.findViewById(R.id.tv_sales);
                viewHolder.mTv_sales_name = (TextView)  convertView.findViewById(R.id.tv_sales_name);
                viewHolder.mTv_car_type = (TextView)  convertView.findViewById(R.id.tv_car_type);
                viewHolder.mTv_client_name_no = (TextView)  convertView.findViewById(R.id.tv_client_name_no);
                viewHolder.mTv_last_time = (TextView)  convertView.findViewById(R.id.tv_last_time);
                viewHolder.iv_tag= (ImageView) convertView.findViewById(R.id.iv_tag);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.mTv_client_name.setText(magOrderLoseData.get(position).getCustomerName());
            if(magOrderLoseData.size()==11) {
                viewHolder.mTv_client_phoneNumber.setText(NumberUtils.parsePhoneNumber(magOrderLoseData.get(position).getCustormerTel()));
            }else{
                viewHolder.mTv_client_phoneNumber.setText(magOrderLoseData.get(position).getCustormerTel());
            }
            viewHolder.mTv_car_type.setText(magOrderLoseData.get(position).getCarModel());
            viewHolder.mTv_last_time.setText(magOrderLoseData.get(position).getCreateTime());
            viewHolder.mTv_sales_name.setText(magOrderLoseData.get(position).getSaleName());
            if(magOrderLoseData.get(position).getIsRead().equals("0")){
                viewHolder.iv_tag.setVisibility(View.VISIBLE);
            }else {
                viewHolder.iv_tag.setVisibility(View.INVISIBLE);
            }
//            viewHolder.ll_client_name.
            return convertView;
        }
    }
    static class ViewHolder {
        private TextView mTv_client_name;
        private TextView mTv_client_phoneNumber;
        private TextView mTv_sales;
        private TextView mTv_sales_name;
        private TextView mTv_car_type;
        private TextView mTv_client_name_no;
        private TextView mTv_last_time;
        private ImageView iv_tag;
    }
}
